package com.zhugang.week04;

/**
 * @program algorithms
 * @description: multiply
 * @author: chanzhugang
 * @create: 2022/06/24 16:12
 */
public class Multiply {

    public static void main(String[] args) {
        Multiply multiply = new Multiply();
        int res = multiply.multiply(3, 4);
        System.out.println(res);
    }

    /**
     * 面试题08.05 递归乘法
     *
     * @param A
     * @param B
     * @return
     */
    public int multiply(int A, int B) {
        // A 个 B 相加
        if (A == 1) {
            return B;
        }
        // (B + B) + (B + B)  + B
        int halfVal = multiply(A / 2, B);
        if (A % 2 == 1) {
            return halfVal + halfVal + B;
        } else {
            return halfVal + halfVal;
        }
    }
}